package model.assessment;

import model.Database;
import model.UID_Generator;
import net.passClass.PassData;
import net.passClass.PassQuestion;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Objects;

public class QuestionDB implements Database<Question> {
    private ArrayList<Question> questions;

    public ArrayList<Question> getQuestions() {
        return questions;
    }
    private static QuestionDB instance = null;
    private QuestionDB() {}
    private QuestionDB(ArrayList<Question> questions) {
        this.questions = Objects.requireNonNullElseGet(questions, ArrayList::new);
    }
    public static QuestionDB getInstance() {
        if (instance == null){
            instance = new QuestionDB();
        }
        return instance;
    }
    public static void initialize(ArrayList<Question> questions) {
        if (instance == null){
            instance = new QuestionDB(questions);
        }
    }

    @Override
    public PassData add(Question question) {
        for(Question aQuestion: questions){
            if(!aQuestion.isDeleted() &&
                    aQuestion.getName().equals(question.getName())){
                return new PassData(false, null,"问题重复，添加失败");
            }
        }
        question.setUid(UID_Generator.getUid("Q"));
        questions.add(question);
        return new PassData(true, null,"添加成功");
    }

    @Override
    public PassData update(Question question){
        if(!questions.contains(question)){
            return new PassData(false, null,"问题不存在，修改失败");
        }
        Question theQuestion = questions.get(questions.indexOf(question));
        theQuestion.setDeleted(true);
        questions.add(question);
        return new PassData(true, null,"修改成功");
    }

    @Override
    public PassData delete(Question question) {
        if(!questions.contains(question)){
            return new PassData(false, null,"问题不存在，删除失败");
        }
        Question theQuestion = questions.get(questions.indexOf(question));
        theQuestion.setDeleted(true);
        return new PassData(true, null,"删除成功");
    }

    @Override
    public PassQuestion get(Question question){
        LinkedList<Question> tmpAns = new LinkedList<>(questions);
        tmpAns.removeIf(Question::isDeleted);

        if(question.getUid()!=null) {
            if (tmpAns.contains(question)) {
                ArrayList<Question> ans = new ArrayList<>();
                ans.add(tmpAns.get(tmpAns.indexOf(question)));
                return new PassQuestion(true, ans, "查找成功");
            } else {
                return new PassQuestion(true, null, "不存在查找结果");
            }
        }

        if(question.getName()!=null){
            tmpAns.removeIf(aQuestion -> !aQuestion.getName()
                    .contains(question.getName()));
        }
        if(question.getAnswers_0()!=null){
            tmpAns.removeIf(aQuestion -> !aQuestion.getAnswers_0()
                    .contains(question.getAnswers_0()));
        }
        if(question.getAnswers_1()!=null){
            tmpAns.removeIf(aQuestion -> !aQuestion.getAnswers_1()
                    .contains(question.getAnswers_1()));
        }
        if(question.getAnswers_2()!=null){
            tmpAns.removeIf(aQuestion -> !aQuestion.getAnswers_2()
                    .contains(question.getAnswers_2()));
        }
        if(question.getType()!=null){
            tmpAns.removeIf(aQuestion -> !aQuestion.getType()
                    .contains(question.getType()));
        }

        ArrayList<Question> ans = new ArrayList<>(tmpAns);
        return new PassQuestion(true, ans,"查找成功");
    }

}
